package com.ruix.app.network;

import android.content.Context;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken;
import com.ruix.app.model.OssToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by hadexs on 2017/4/28.
 */

public class ALUploadFileParam {
    private String uid; //用户id
    private ArrayList<String> pathes; //图片路径
    private OssToken ossToken;
    private OSS ossClient;

    //todo


    public ALUploadFileParam() {
    }

    public ALUploadFileParam(String uid, ArrayList<String> pathes) {
        this.uid = uid;
        this.pathes = pathes;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public ArrayList<String> getPathes() {
        return pathes;
    }

    public void setPathes(ArrayList<String> pathes) {
        this.pathes = pathes;
    }

    public void addPathe(String pathe) {
        if (pathes == null) {
            pathes = new ArrayList<>();
        }
        pathes.add(pathe);
    }

    public OSS getOssClient() {
        return ossClient;
    }

    public void setOssClient(OSS ossClient) {
        this.ossClient = ossClient;
    }

    public OssToken getOssToken() {
        return ossToken;
    }

    public void setOssToken(OssToken ossToken) {
        this.ossToken = ossToken;
    }

    public boolean canUpload() {
        return uid != null && pathes != null && ossToken != null && ossClient != null && pathes.size() > 0;
    }

    //获取文件名
    public String getObjectName(String path) {
        String reuslt = ossToken.getUploadFilePath() + uid + "_" + System.currentTimeMillis() + "_" + new Random().nextInt(1000);
        String type = null;
        if (path.contains(".")) {
            type = path.substring(path.lastIndexOf("."));
        }
        if (type != null) {
            reuslt += type;
        }
        return reuslt;
    }


    //TODO
    public static OSS loadOSSClient(Context context, final OssToken ossToken) {

        OSSCredentialProvider credentialProvider = new OSSFederationCredentialProvider() {
            @Override
            public OSSFederationToken getFederationToken() {
                return new OSSFederationToken(ossToken.getAccessKeyId(), ossToken.getAccessKeySecret(), ossToken.getSecurityToken(), ossToken.getExpiration());
            }
        };

        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次
        OSSLog.enableLog();
        return new OSSClient(context.getApplicationContext(), ossToken.getEndpoint(), credentialProvider, conf);
    }
}
